package com.pray.praymall.member.inteceptor;

import com.pray.common.constant.AuthServerConstant;
import com.pray.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * do something
 *
 * @author 魂悸魄
 * @date 2020/9/25 21:07
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 远程调用无需登录的，直接放行
        // /order/order/status/{orderSn}
        boolean match = new AntPathMatcher().match("/member/**", request.getRequestURI());
        if (match) {
            return true;
        }
        MemberResponseVo vo = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (vo == null) {
            request.getSession().setAttribute("msg", "请先进行登录");
            response.sendRedirect("http://auth.praymall.com/login.html");
            return false;
        }
        loginUser.set(vo);
        return true;
    }
}
